package lumis.service.portalmanagement.cluster;

import java.util.Collection;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.cluster.IClusterManager;
import lumis.portal.manager.ManagerFactory;
import lumis.util.ITransaction;
import lumis.util.PortalUtil;
/**
 * 
 * Provides cluster members data.
 *
 * @version $Revision: 7564 $ $Date: 2007-06-27 10:11:46 -0300 (Wed, 27 Jun 2007) $
 * @since 4.0.11
 */
public class ClusterMemberDataProvider implements IDataProvider<TabularSource<?>> 
{
	public void loadData(SessionConfig sessionConfig, TabularSource<?> source, ITransaction transaction) throws PortalException
	{
		IClusterManager clusterManager = ManagerFactory.getClusterManager();
		Collection members = clusterManager.getMembers();
		
		TabularData tabularData = source.getData();
		for(Object member: members)
		{
			String memberStr = member.toString();
			int index = memberStr.indexOf(":");
			if (index != -1)
			{
				ISourceData newRow = tabularData.addRow();
				
				String ipAddress =  memberStr.substring(0, index);
				String port = memberStr.substring(index+1, memberStr.length());
			
				newRow.put("id", PortalUtil.generateNewGuid());
				newRow.put("ipAddress", ipAddress);
				newRow.put("port", port);
			}
		}
	}
}